<template>
  <div class="app-container">
    <RoomTree v-model="currentTree" @change="treeClick" />
    <div class="container_body">
      <el-row :gutter="10" class="mb8">
        <el-button type="primary" icon="el-icon-refresh-left" size="mini" @click="getList">刷新</el-button>
        <el-button
          v-hasPermi="['base:room:device:set']"
          type="info"
          icon="el-icon-odometer"
          :disabled="single"
          size="mini"
          @click="handleSetting"
          >设置</el-button
        >
        <el-button type="danger" :disabled="multiple" v-hasPermi="['param:eq:del']" icon="el-icon-odometer" size="mini"
          @click="handleDelete">删除</el-button
        >
        <el-button
          v-hasPermi="['param:eq:call']"
          type="warning"
          icon="el-icon-odometer"
          size="mini"
          :disabled="multiple"
          @click="zhaoce"
          >召测</el-button
        >
      </el-row>

      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="68px"
      >
        <!-- <el-form-item label="学校名称" prop="sId">
            <FusionSelect type="school" v-model="queryParams.sId" />
          </el-form-item>
          <el-form-item label="校区名称" prop="aId">
            <FusionSelect type="area" :instantly="false" paramsKey="pid" :paramsVal="queryParams.sId" v-model="queryParams.aId" />
          </el-form-item>
          <el-form-item label="楼座名称" prop="pId">
            <FusionSelect type="build" :instantly="false" paramsKey="pid2" :paramsVal="queryParams.aId" v-model="queryParams.pId" />
          </el-form-item>
          <el-form-item label="所属楼层" prop="floor">
            <FusionSelect type="floor" :instantly="false" paramsKey="build" :paramsVal="queryParams.pId" v-model="queryParams.floor" />
        </el-form-item>-->
        <el-form-item label="房间号码" prop="roomId">
          <RoomSelect v-model="queryParams.roomId" :params="roomSelectParams" />
        </el-form-item>
        <el-form-item label="通讯状态" prop="runStatus">
          <el-select
            v-model="queryParams.runStatus"
            placeholder="通讯状态"
            clearable
            style="width: 240px"
          >
            <el-option label="全部" value=""></el-option>
            <el-option
              v-for="dict in dict.type.run_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="hteqList"
        @selection-change="handleSelectionChange"
        row-key="roomId"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="房间号码" align="center" prop="roomNo" width="120" />
        <el-table-column label="设备编号" align="center" prop="meterNo" />
        <el-table-column label="通讯状态" align="center" prop="runStatus">
          <template slot-scope="scope">
            <dict-tag
              :value="scope.row.runStatus"
              :options="dict.type.run_status"
            />
          </template>
        </el-table-column>
        <el-table-column label="总用电量" align="center" prop="totalEq" />
        <el-table-column
          label="月结转累计电量"
          align="center"
          prop="monJzTotalEq"
        />
        <el-table-column label="月结转电量" align="center" prop="monJzEq" />
        <el-table-column label="免费电量" align="center" prop="freeEq" />
        <el-table-column label="充值电量" align="center" prop="rechargeEq" />
        <el-table-column label="已用电量" align="center" prop="useEq" />
        <el-table-column label="剩余电量" align="center" prop="remainEq" />
        <el-table-column label="透支电量" align="center" prop="tzEq" />
        <el-table-column label="更新时间" align="center" prop="updateDt" width="150">
          <template slot-scope="scope">{{ formatterDt(scope.row.updateDt) }}</template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 设置弹窗 -->
    <el-dialog
      title="设置电量参数"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-row>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-col :span="12">
            <el-form-item label="房间号码" prop="roomNo">
              <el-input
                v-model="form.roomNo"
                placeholder="请输入房间号码"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="meterNo">
              <el-input
                v-model="form.meterNo"
                placeholder="请输入设备编号"
              /> </el-form-item
          ></el-col>

          <el-col :span="12">
            <el-form-item label="总用电量" prop="totalEq">
              <el-input
                v-model="form.totalEq"
                readonly
                placeholder="请输入总用电量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月结转累计电量" prop="monJzTotalEq">
              <el-input
                v-model="form.monJzTotalEq"
                readonly
                placeholder="请输入月结转累计电量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月结转电量" prop="monJzEq">
              <el-input
                v-model="form.monJzEq"
                readonly
                placeholder="请输入月结转电量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月结转电量" prop="monJzEq">
              <el-input
                v-model="form.monJzEq"
                readonly
                placeholder="请输入月结转电量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="免费电量" prop="freeEq">
              <el-input
                v-model="form.freeEq"
                readonly
                placeholder="请输入免费电量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值电量" prop="rechargeEq">
              <el-input
                v-model="form.rechargeEq"
                readonly
                placeholder="请输入充值电量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已用电量" prop="useEq">
              <el-input
                v-model="form.useEq"
                readonly
                placeholder="请输入已用电量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剩余电量" prop="remainEq">
              <el-input
                v-model="form.remainEq"
                readonly
                placeholder="请输入剩余电量"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="透支电量" prop="tzEq">
              <el-input
                v-model="form.tzEq"
                placeholder="请输入透支电量"
              /> </el-form-item
          ></el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 召测 -->
    <ResultStatuListModal ref="ResultStatuListModal" @refresh="getList" />
  </div>
</template>
<script>
import { listHteq, eqSet, eqCall, eqDel } from "@/api/electricity/parameter";
import RoomTree from "@/components/RoomTree";
import FusionSelect from "@/components/FusionSelect";
import RoomSelect from "@/components/RoomSelect";
import ResultStatuListModal from "@/components/ResultStatuListModal";
import moment from "moment";
export default {
  name: "Parameter",
  dicts: ["run_status"],
  components: {
    RoomTree,
    RoomSelect,
    FusionSelect,
    ResultStatuListModal,
  },
  data() {
    return {
      // 当前选中层级
      currentTree: {},
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      hteqList: [],
      // 设置弹窗
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sId: [{ required: true, message: "学校不能为空", trigger: "blur" }],
      },
      selection: [],
      roomSelectParams: {},
      getListParams: {},
      meterNos: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatterDt(date) {
      return moment(date).format("yyyy-MM-DD HH:mm:ss");
    },
    getList() {
      this.loading = true;
      listHteq({
        ...this.queryParams,
        ...this.getListParams
      }).then((response) => {
        this.hteqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
      this.ids = selection.map(item => item.roomId);
      this.meterNos = selection.map(item => item.meterNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 设置
    handleSetting() {
      this.form = this.selection[0];
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        buildId: null,
        floorNo: null,
        roomNo: null,
        meterNo: null,
        tzEq: null,
        remainEq: null,
        useEq: null,
        rechargeEq: null,
        freeEq: null,
        monJzEq: null,
        monJzTotalEq: null,
        totalEq: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.form.updateDt = moment(this.form.updateDt).format("yyyy-MM-DD HH:mm:ss")
          delete this.form.updateDt;
          eqSet(this.form).then((response) => {
            this.$modal.msgSuccess("设置成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    handleDelete() {
      const ids = this.ids;
      this.$modal
        .confirm("是否确认删除该房间电量参数的数据项？")
        .then(function() {
          return eqDel({roomIds: ids.toString()});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    zhaoce() {
      this.$refs.ResultStatuListModal.show({
        title: "您确定要召测数据？",
        actionName: '召测',
        total: this.selection.length,
        handler: (onItemOver) => eqCall({ rooms: this.selection, meterNos: this.meterNos, onItemOver })
      });
    },
    treeClick() {
      const nameMap = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.roomSelectParams = {
        [nameMap[this.currentTree.name]]: this.currentTree.id
      };
      const nameMap2 = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.getListParams = {
        [nameMap2[this.currentTree.name]]: this.currentTree.id
          // this.currentTree.name == "floor"
          //   ? this.currentTree.data.name.substring(0, 1)
          //   : this.currentTree.id
      };
      this.handleQuery();
    }
  },
};
</script>  

<style lang="scss" scoped>
.app-container {
  display: flex;
  overflow: hidden;
  .container_body {
    box-sizing: content-box;
    flex: 1;
    max-height: 100%;
    overflow: auto;
    padding-left: 10px;
    padding-right: 5px;
  }
}
</style>
